package com.kunminx.viabus.ui;

import com.kunminx.architecture.base.BaseBusAbility;
import com.kunminx.architecture.utils.PermissionUtils;
import com.kunminx.core.bus.Result;
import com.kunminx.core.myUtils.utils.Log;
import com.kunminx.viabus.utils.TextUtils;
import com.kunminx.viabus.business.NoteBusiness;
import com.kunminx.viabus.business.bus.NoteBus;
import ohos.aafwk.content.Intent;
import ohos.agp.window.dialog.ToastDialog;
import ohos.security.SystemPermission;

/**
 * @author KunMinX
 * Create at 2018/8/21
 */
public class MainAbility extends BaseBusAbility implements PermissionUtils.IPermissionCallback {

    private NoteBusiness mNoteBusiness;


    /**
     * onStart
     *
     * @param intent
     */
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        initBusiness();
        setMainRoute(TodoListSlice.class.getName());
    }

    /*
     * initBusiness
     */
    private void initBusiness() {
        mNoteBusiness = new NoteBusiness();
        mNoteBusiness.init(MainAbility.this);
        NoteBus.registerRequestHandler(mNoteBusiness);
        PermissionUtils.requestPermissionInAbility(this, this, SystemPermission.WRITE_USER_STORAGE);
    }

    /**
     * onStop
     */
    @Override
    protected void onStop() {
        super.onStop();
        NoteBus.unregisterRequestHandler(mNoteBusiness);
        mNoteBusiness = null;
    }

    /**
     * onResultHandle
     *
     * @param testResult
     */
    @Override
    public void onResultHandle(Result testResult) {

    }

    /**
     * onBackPressed
     */
    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    /**
     * PermissionUtils.requestPermissionInAbility onResult回调
     *
     * @param msg
     */
    @Override
    public void onResult(String msg) {
        if (TextUtils.isEmpty(msg)) {
            // 这里的东西放到了initBusines上面，因为文件结构不同这样才能正常
            Log.i("正常");
        } else {
            new ToastDialog(getContext())
                    .setText("权限异常")
                    .show();
        }
    }
}
